package campshop.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import campshop.models.SchoolHotGoods;
public class SchoolHotGoodsDao extends JdbcDaoSupport{
	public boolean addSchoolHotGoodsDao(SchoolHotGoods shg)
	{
		String sql = "insert into " +
				"tbl_school_hot_goods (gi_uGoodsId, sr_uSchoolId, shg_uSort)" +
				" values (?, ?, ?)";
		if(super.getJdbcTemplate().update(sql, shg.getuGoodsId(), shg.getuSchoolId(), shg.getiSort())>0){
			return true;
		}else return false;
	}
	
	public List<SchoolHotGoods> getHotGoodsListBySchool(long uSchoolId) {
		String sql = "select * from tbl_school_hot_goods where sr_uSchoolId="+ uSchoolId +"order by shg_uSort asc";
		return super.getJdbcTemplate().query(sql, new SchoolHotGoodsRowMapper());
	}
	
	class SchoolHotGoodsRowMapper implements RowMapper<SchoolHotGoods>{
		@Override
		public SchoolHotGoods mapRow(ResultSet rs, int rowNum) throws SQLException {
			SchoolHotGoods shg = new SchoolHotGoods();
			shg.setuGoodsId(rs.getLong("gi_uGoodsId"));
			shg.setuSchoolId(rs.getLong("sr_uSchoolId"));
			shg.setiSort(rs.getInt("shg_uSort"));
			return shg;
		}	
	}
}	